/**
 * Copyright (C),2011-2012,北京国电通网络技术有限公司.
 * FileName:	protocol.cpp
 * Author:		LiuHaifeng
 * Version:		V0.1
 * Date:		2011-11-15
 * Description	this file implements all kinds of protocol change, include up-stream and down-stream
 */

/**
 * History:
 * 		Author		date		Version		Description
 * 	  LiuHaifeng  2012-03-10	V1.0.5     add light and curtain socketID validate
 * 	  LiYang	  2012-03-10	V1.0.5     modification time task
 */

#include "protocol.h"
#include "../serial/serial.h"
#include "../sqlite/sqlite.h"

extern struct StEquipList g_devList;
extern struct StDownTask g_downTask;
extern struct StUpTask g_upTask;
extern struct StDataTransmit g_dataTrans;
extern struct StDeadTimeTask g_deadTimeTask;
extern struct StOther g_other;
extern struct StDelay g_sendDelay;
extern int flagAddress;



/***commandDispose() brief implement down-stream protocol change
 * param position[in], position of struct StControlCmd to be changed
 *
 * retval int,0 if successfully or -1 failed
 ***/
int commandDispose(int pos)
{
	PRINTOUT("%s","in commandDispose().");
	int l_devPos=-1;
	int l_sendNum=0;										///the number send to serial
	char l_tempStr[32];
	int l_tmpTime;
	int l_addMode=1;
	if(0x01==g_downTask.st_command[pos].cmdType)			///control kind
	{
		PRINTOUT("%s","control-kind command.");
		///copy Command to 'g_dataTrans.st_sendService'
		if(1==g_downTask.st_command[pos].otherParam)		///add time-task
		{
			PRINTOUT("%s","add time-task.");
			if(-1==addTask(pos))							///add to sqlite-database
			{
				ERROROUT("add time-task failed!");
				goto databaseFAILED;
			}
			else
				sendService(err_no);
		}
		else if(2==g_downTask.st_command[pos].otherParam)	///model set
		{
			PRINTOUT("%s","set model.");
			l_addMode=addMode(pos);
			if(-1==l_addMode)							///add to sqlite-database
			{
				ERROROUT("set model failed!");
				goto databaseFAILED;
			}
			else if (-2==l_addMode)
			{
				ERROROUT("this mode have exist!");
				goto socketFAILED;
			}
			else
				sendService(err_no);
		}
		else												///if no, just immediate task
		{
			if(0xfe==g_downTask.st_command[pos].param[0])	///mode operate
			{
				PRINTOUT("%s","model operation command.");
				if(-1==modelOper(g_downTask.st_command[pos].param[1]))
				{
					ERROROUT("model operate failed!");
					goto databaseFAILED;
				}
				else
					sendService(err_no);
			}
			else if(0x80 > g_downTask.st_command[pos].typeID[0])	///normal operation
			{
				PRINTOUT("%s","'State Grid' Control.");
				sem_wait(&g_devList.st_equip.st_sem);///!!!!!
				l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
				if(-1==l_devPos)
				{
					ERROROUT("NO this device!");
					sem_post(&g_devList.st_equip.st_sem);///!!!!!
					goto socketFAILED;
				}
				if(g_devList.st_equipArray[l_devPos].equipmentID[0]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[1]==0x00 \
						&& g_devList.st_equipArray[l_devPos].equipmentID[2]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[3]==0x00\
						&& g_devList.st_equipArray[l_devPos].equipmentID[4]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[5]==0x00)
				///make sure socket's address mustn't empty
				{
					ERROROUT("device have no correspond socket!");
					sem_post(&g_devList.st_equip.st_sem);///!!!!!
					goto socketFAILED;
				}
				g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
				g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				if(0x03==g_downTask.st_command[pos].typeID[1])			///light-control device
				{
					printf("light_control\n");
					g_dataTrans.st_sendSerial[l_sendNum++]=0x04;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
					if(0x01==g_downTask.st_command[pos].param[0])		///on-off
					{
						if(0x00==g_downTask.st_command[pos].param[1])			///off
						{
							g_dataTrans.st_sendSerial[l_sendNum++]=0xe5;
							g_dataTrans.st_sendSerial[l_sendNum++]=0x34;
						}
						else if(0x01==g_downTask.st_command[pos].param[1])		///on
						{
							g_dataTrans.st_sendSerial[l_sendNum++]=0xe4;
							g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						}
						else
						{
							ERROROUT("no this device operation!");
							goto formatFAILED;
						}
					}
					else if(0x02==g_downTask.st_command[pos].param[0])			///light+
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0xe4;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x43;
					}
					else if(0x03==g_downTask.st_command[pos].param[0])			///light-
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0xe4;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x44;
					}
					else if(0x04==g_downTask.st_command[pos].param[0])			///light++
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0xe4;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x45;
					}
					else if(0x05==g_downTask.st_command[pos].param[0])			///light--
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0xe4;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x46;
					}
					else
					{
						ERROROUT("no this device operation!");
						goto formatFAILED;
					}
					g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
					g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
					++l_sendNum;
					goto lightCONTROL;
				}
				else if(0x06==g_downTask.st_command[pos].typeID[1])		///curtain
				{
					g_dataTrans.st_sendSerial[l_sendNum++]=0x04;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
					g_dataTrans.st_sendSerial[l_sendNum++]=0xe3;
					if(0x02==g_downTask.st_command[pos].param[0])				///stop
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0x35;
					}
					else if(0x03==g_downTask.st_command[pos].param[0])			///forward
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
					}
					else if(0x04==g_downTask.st_command[pos].param[0])			///backward
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0x34;
					}
					else
					{
						ERROROUT("no this device operation!");
						goto formatFAILED;;
					}
				}
				else													///normal device
				{
					if(0x01==g_downTask.st_command[pos].param[0])				///on-off
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0x04;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x09;
						if(0x01==g_downTask.st_command[pos].param[1])			///on
						{
							g_dataTrans.st_sendSerial[l_sendNum++]=0x56;
						}
						else if(0x00==g_downTask.st_command[pos].param[1])		///off
						{
							g_dataTrans.st_sendSerial[l_sendNum++]=0x55;
						}
						g_dataTrans.st_sendSerial[l_sendNum++]=0x16;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
					}
					else
					{
						ERROROUT("no this device operation!");
						goto formatFAILED;;
					}
				}
				g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
				g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
				++l_sendNum;
			}
			else if(0x82==g_downTask.st_command[pos].typeID[0])	///Media-美的
			{
				PRINTOUT("%s","'Media' Control.");
				sem_wait(&g_devList.st_equip.st_sem);///!!!!!
				l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
				if(-1==l_devPos)
				{
					ERROROUT("NO this device!");
					sem_post(&g_devList.st_equip.st_sem);///!!!!!
					goto socketFAILED;
				}
				///if open Media device, open correspond socket first
				if(0x01==g_downTask.st_command[pos].param[0] && 0x01==g_downTask.st_command[pos].param[1] && 1!=g_devList.st_equipArray[l_devPos].socketStatus)
				{
					if(g_devList.st_equipArray[l_devPos].equipmentID[0]!=0x00 || g_devList.st_equipArray[l_devPos].equipmentID[1]!=0x00 \
							|| g_devList.st_equipArray[l_devPos].equipmentID[2]!=0x00 || g_devList.st_equipArray[l_devPos].equipmentID[3]!=0x00\
							|| g_devList.st_equipArray[l_devPos].equipmentID[4]!=0x00 || g_devList.st_equipArray[l_devPos].equipmentID[5]!=0x00)
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
						g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x04;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x09;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x56;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x16;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
						g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
						++l_sendNum;
						myDelay(needDelay(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2]));
						if(!sendSerial(l_sendNum))
						{
							ERROROUT("sendSerial() failed!");
							sem_post(&g_devList.st_equip.st_sem);///!!!!!
							goto serialFAILED;
						}
						myDelay(300);		///???bug???make sure relay has acted
						l_sendNum=0;
					}
				}
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				///Media operation
				g_dataTrans.st_sendSerial[l_sendNum++]=0x0b;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x03;
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].operID	& 0xff;
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].operID>>8 & 0xff;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x34;								///???may change???the address of family
				g_dataTrans.st_sendSerial[l_sendNum++]=0x12;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x01;								/// the number of parameter
				g_dataTrans.st_sendSerial[l_sendNum++]=g_downTask.st_command[pos].param[0];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_downTask.st_command[pos].param[1];
				///if close Media device, close correspond socket
				if(0x01==g_downTask.st_command[pos].param[0] && 0x00==g_downTask.st_command[pos].param[1] && 1==g_devList.st_equipArray[l_devPos].socketStatus)
				{
					if(0!=l_sendNum)										///have data to send
					{
						myDelay(needDelay(0));
						if(!sendSerial(l_sendNum))
						{
							ERROROUT("sendSerial() failed!");
							goto serialFAILED;
						}
					}
					myDelay(3200);		///???bug???make sure relay has acted
					l_sendNum=0;
					sem_post(&g_devList.st_equip.st_sem);///!!!!!
					if(g_devList.st_equipArray[l_devPos].equipmentID[0]!=0x00 || g_devList.st_equipArray[l_devPos].equipmentID[1]!=0x00 \
							|| g_devList.st_equipArray[l_devPos].equipmentID[2]!=0x00 || g_devList.st_equipArray[l_devPos].equipmentID[3]!=0x00\
							|| g_devList.st_equipArray[l_devPos].equipmentID[4]!=0x00 || g_devList.st_equipArray[l_devPos].equipmentID[5]!=0x00)
					{
						g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
						g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
						g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x04;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x09;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x55;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x16;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum++]=0x33;
						g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
						g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
						++l_sendNum;
						myDelay(needDelay(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2]));
						if(!sendSerial(l_sendNum))
						{
							ERROROUT("sendSerial() failed!");
							goto serialFAILED;
						}
						l_sendNum=0;
					}
				}
			}
			else
			{
				PRINTOUT("%s","NO this operation command!");
				goto formatFAILED;
			}
		}
	}
	else if(0x02==g_downTask.st_command[pos].cmdType)		///inquiry kind
	{
		PRINTOUT("%s","inquiry kind command.");
		if(0x06==g_downTask.st_command[pos].param[0])		///Media inquiry
		{
			PRINTOUT("%s","'Media' inquiry.");
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1==l_devPos)
			{
				ERROROUT("NO this device!");
				goto socketFAILED;
			}
			g_dataTrans.st_sendSerial[l_sendNum++]=0x0b;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x05;
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].operID & 0xff;
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].operID>>8 & 0xff;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x34;								///the address of family
			g_dataTrans.st_sendSerial[l_sendNum++]=0x12;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x01;								/// the number of parameter
			g_dataTrans.st_sendSerial[l_sendNum++]=g_downTask.st_command[pos].param[1];
			g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
		}
		else if(0x08==g_downTask.st_command[pos].param[0])
		{
			if(0x82==g_downTask.st_command[pos].typeID[0])  	///Media device OperID inquiry
			{
				PRINTOUT("%s","'Media OperID' inquiry.");
				g_dataTrans.st_sendSerial[l_sendNum++]=0x09;
				g_dataTrans.st_sendSerial[l_sendNum++]=0xf0;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x34;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x12;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
			}
		}
		else if(0x07==g_downTask.st_command[pos].param[0])	///socket validate and light-control bind
		{
			if(0x02==g_downTask.st_command[pos].param[1])								///socketID validate
			{
				PRINTOUT("%s","socket address verify.");
				if('#'==g_other.st_pCmdOther[12])
				{
					g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
					memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[2],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[4],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[6],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[8],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[10],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					if(-1!=findByPoint(&g_dataTrans.st_sendSerial[1],find_socketID))
					{
						ERROROUT("socket have exist!");
						goto socketFAILED;
					}
					g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x63;
					g_dataTrans.st_sendSerial[l_sendNum++]=0xe9;
					g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
					g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
					l_sendNum++;
					sendService(err_no);  //charlie added 
				}
				else
				{
					ERROROUT("socket format error!");
					goto formatFAILED;
				}
			}
			else if(0x01==g_downTask.st_command[pos].param[1])							///light-control bind
			{
				PRINTOUT("%s","light-control bind.");
				;																		///do nothing
			}
			/* 2012-03-10 V1.0.5 modify by LiuHaifeng begin */
			else if(0x03==g_downTask.st_command[pos].param[1])
			{
				PRINTOUT("%s","light address verify.");
				if('#'==g_other.st_pCmdOther[12])  			/////socketID
				{
					g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
					memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[2],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[4],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[6],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[8],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[10],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x04;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
					g_dataTrans.st_sendSerial[l_sendNum++]=0xe4;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x35;
					g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
					g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
					l_sendNum++;
					goto lightCONTROL;
				}
				else
				{
					ERROROUT("socket format error!");
					goto formatFAILED;
				}
			}
			///add curtain socketID validate
			else if(0x04==g_downTask.st_command[pos].param[1])
			{
				PRINTOUT("%s","curtain address verify.");
				if('#'==g_other.st_pCmdOther[12])  			/////??????12?????
				{

				}
				else
				{
					ERROROUT("socket format error!");
					goto formatFAILED;
				}
			}
			/* 2012-03-10 V1.0.5 modify by LiuHaifeng end */
		}
		else if(0x05==g_downTask.st_command[pos].param[0])	///synchronous devlist
		{
			PRINTOUT("%s","synchronous devlist.");
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			if(-1==devSyn())
			{
				ERROROUT("synchronous devlist failed!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto databaseFAILED;
			}
			else
				sendService(send_only);													///send result
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
		}
		else if(0xfe==g_downTask.st_command[pos].param[0])	///mode inquiry
		{
			PRINTOUT("%s","mode inquiry.");
			if(-1==modelQuery(pos))
			{
				ERROROUT("mode inquiry failed!");
				goto databaseFAILED;
			}
			else
				sendService(send_only);
		}
		else if(0xfd==g_downTask.st_command[pos].param[0])	///total-power inquiry-inside----68 a0 a1 a2 a3 a4 a5 68 01 02 43 C3 cs 16
		{
			PRINTOUT("%s","total-power inquiry by socket.");
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1==l_devPos)
			{
				ERROROUT("NO this device!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			if(g_devList.st_equipArray[l_devPos].equipmentID[0]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[1]==0x00 \
					&& g_devList.st_equipArray[l_devPos].equipmentID[2]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[3]==0x00\
					&& g_devList.st_equipArray[l_devPos].equipmentID[4]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[5]==0x00)
			///make sure socket's address mustn't empty
			{
				ERROROUT("device have no correspond socket!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x43;
			g_dataTrans.st_sendSerial[l_sendNum++]=0xc3;
			g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
			g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
			l_sendNum++;
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
		}
		else if(0xfc==g_downTask.st_command[pos].param[0])	///status inquiry-inside----68 a0 a1 a2 a3 a4 a5 68 01 02 93 E9 cs 16
		{
			PRINTOUT("%s","status inquiry by socket.");
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1==l_devPos)
			{
				ERROROUT("NO this device!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			if(g_devList.st_equipArray[l_devPos].equipmentID[0]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[1]==0x00 \
					&& g_devList.st_equipArray[l_devPos].equipmentID[2]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[3]==0x00\
					&& g_devList.st_equipArray[l_devPos].equipmentID[4]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[5]==0x00)
			///make sure socket's address mustn't empty
			{
				ERROROUT("device have no correspond socket!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x93;
			g_dataTrans.st_sendSerial[l_sendNum++]=0xe9;
			g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
			g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
			l_sendNum++;
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
		}
		else if(0xfb==g_downTask.st_command[pos].param[0])	///active power inquiry -inside
		{
			PRINTOUT("%s","active power inquiry by socket.");
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1==l_devPos)
			{
				ERROROUT("NO this device!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			if(g_devList.st_equipArray[l_devPos].equipmentID[0]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[1]==0x00 \
					&& g_devList.st_equipArray[l_devPos].equipmentID[2]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[3]==0x00\
					&& g_devList.st_equipArray[l_devPos].equipmentID[4]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[5]==0x00)
			///make sure socket's address mustn't empty
			{
				ERROROUT("device have no correspond socket!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x63;
			g_dataTrans.st_sendSerial[l_sendNum++]=0xe9;
			g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
			g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
			l_sendNum++;
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
		}
		else if(0x02==g_downTask.st_command[pos].param[0] && 0x01==g_downTask.st_command[pos].param[1] && 0x00==g_downTask.st_command[pos].otherParam \
								&& g_downTask.st_command[pos].typeID[0]!=0x00 && g_downTask.st_command[pos].typeID[1]!=0x00 && g_downTask.st_command[pos].typeID[2]!=0x00) ///active power inquiry
		{
			PRINTOUT("%s","single device active power inquiry.");
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1==l_devPos)
			{
				ERROROUT("NO this device!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			if(g_devList.st_equipArray[l_devPos].equipmentID[0]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[1]==0x00 \
					&& g_devList.st_equipArray[l_devPos].equipmentID[2]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[3]==0x00\
					&& g_devList.st_equipArray[l_devPos].equipmentID[4]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[5]==0x00)
			///make sure socket's address mustn't empty
			{
				ERROROUT("device have no correspond socket!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
			g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
			g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
			g_dataTrans.st_sendSerial[l_sendNum++]=0x63;
			g_dataTrans.st_sendSerial[l_sendNum++]=0xe9;
			g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
			g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
			l_sendNum++;
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
		}
		else if(0x02==g_downTask.st_command[pos].param[0] && 0x01==g_downTask.st_command[pos].param[1]  && 0x03==g_downTask.st_command[pos].otherParam \
						&& g_downTask.st_command[pos].typeID[0]==0x00 && g_downTask.st_command[pos].typeID[1]==0x00 && g_downTask.st_command[pos].typeID[2]==0x00)
		{
			PRINTOUT("%s","inquiry all device's active-power between begin-time to end-time.");
			if(-1==(l_sendNum=inquiryActivePower(pos,-1,l_sendNum)))
			{
				ERROROUT("active-power inquiry failed!");
				goto databaseFAILED;
			}
			g_other.st_pCmdOther[l_sendNum++]='#';
			g_other.st_pCmdOther[l_sendNum]='\0';
			sendService(send_only);
			l_sendNum=0;
		}
		///inquiry all device's time-task
		else if(0x04==g_downTask.st_command[pos].param[0] && g_downTask.st_command[pos].typeID[0]==0x00 \
				&& g_downTask.st_command[pos].typeID[1]==0x00 && g_downTask.st_command[pos].typeID[2]==0x00)
		{
			PRINTOUT("%s","inquiry all device's time-task.");
			if(-1==(l_sendNum=tmTaskQuery(pos,-1,l_sendNum)))
			{
				ERROROUT("time-task inquiry failed!");
				goto databaseFAILED;
			}
			g_other.st_pCmdOther[l_sendNum++]='#';
			g_other.st_pCmdOther[l_sendNum]='\0';
			sendService(send_only);
			l_sendNum=0;
		}
		///inquiry all device's total elec-power in hour
		else if(0x03==g_downTask.st_command[pos].param[0] && 0x01==g_downTask.st_command[pos].param[1]  \
				&& g_downTask.st_command[pos].typeID[0]==0x00 && g_downTask.st_command[pos].typeID[1]==0x00 && g_downTask.st_command[pos].typeID[2]==0x00)
		{
			PRINTOUT("%s","inquiry all device's total elec-powr.");
			if(-1==(l_sendNum=inquiryElecPower(pos,-1,l_sendNum)))
			{
				ERROROUT("total elec-power inquiry failed!");
				goto databaseFAILED;
			}
			g_other.st_pCmdOther[l_sendNum++]='#';
			g_other.st_pCmdOther[l_sendNum]='\0';
			sendService(send_only);
			l_sendNum=0;
		}
		else
		{
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			for(int i=g_devList.st_equip.head;i!=-1;i=g_devList.st_equipArray[i].next)
			{
				if(((g_devList.st_equipArray[i].typeID >>16 & 0xff) == g_downTask.st_command[pos].typeID[0] || 0x00==g_downTask.st_command[pos].typeID[0])\
						&& ((g_devList.st_equipArray[i].typeID >>8 & 0xff) == g_downTask.st_command[pos].typeID[1] || 0x00==g_downTask.st_command[pos].typeID[1]) \
						&& ((g_devList.st_equipArray[i].typeID & 0xff) == g_downTask.st_command[pos].typeID[2] || 0x00==g_downTask.st_command[pos].typeID[2]))
				{
					switch(g_downTask.st_command[pos].param[0])
					{
					case 0x01:			///on-off status
						PRINTOUT("%s","on-off status inquiry.");
						sprintf(l_tempStr,"%02x%02x%02x",g_devList.st_equipArray[i].typeID>>16 & 0xff ,g_devList.st_equipArray[i].typeID>>8 & 0xff,g_devList.st_equipArray[i].typeID & 0xff);
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
						g_other.st_pCmdOther[l_sendNum++]=';';
						sprintf(l_tempStr,"%02d",g_devList.st_equipArray[i].socketStatus);
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
						g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
						g_other.st_pCmdOther[l_sendNum++]=';';
						g_other.st_pCmdOther[l_sendNum++]='@';
						break;
					case 0x03:			///elec total-power
						PRINTOUT("%s","elec total-power inquiry for single.");
						if(-1==(l_sendNum=inquiryElecPower(pos,i,l_sendNum)))
						{
							ERROROUT("elec total-power inquiry failed!");
							sem_post(&g_devList.st_equip.st_sem);///!!!!!
							goto databaseFAILED;
						}
						break;
					case 0x04:			///time-task inquiry
						PRINTOUT("%s","time-task inquiry.");
						if(0x01==g_downTask.st_command[pos].param[1])		///inquiry time-task
						{
							if(-1==(l_sendNum=tmTaskQuery(pos,i,l_sendNum)))
							{
								ERROROUT("time-task inquiry failed!");
								sem_post(&g_devList.st_equip.st_sem);///!!!!!
								goto databaseFAILED;
							}
						}
						else
						{
							ERROROUT("param error!");
							sem_post(&g_devList.st_equip.st_sem);///!!!!!
							goto formatFAILED;
						}
						break;
					default:
						PRINTOUT("%s","no this inquiry command.");
						break;
					}
				}
			}
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
			g_other.st_pCmdOther[l_sendNum++]='#';
			g_other.st_pCmdOther[l_sendNum]='\0';
			sendService(send_only);
			l_sendNum=0;
		}
	}
	else if(0x03==g_downTask.st_command[pos].cmdType)		///operate kind
	{
		PRINTOUT("%s","operate kind command.");
		if(0x01==g_downTask.st_command[pos].param[0])		///add device
		{
			PRINTOUT("%s","add device.");
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			if(-1==g_devList.st_empty.head)
			{
				ERROROUT("too more devices!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto memoryFAILED;
			}
			///see if typeID exist???
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1!=l_devPos)
			{
				ERROROUT("already have this device!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			///see if socketAddress exist???
			if(0x02!=g_downTask.st_command[pos].param[1])
			{
				unsigned char l_socketAddress[7];
				char * l_ch;
				if(0x01==g_downTask.st_command[pos].param[1])		///socketID
					l_ch = &g_other.st_shmADDR[28];
				else if(0x03==g_downTask.st_command[pos].param[1])
					l_ch = &g_other.st_shmADDR[32];
				l_socketAddress[0]=charToUnsigned(&l_ch);
				l_socketAddress[1]=charToUnsigned(&l_ch);
				l_socketAddress[2]=charToUnsigned(&l_ch);
				l_socketAddress[3]=charToUnsigned(&l_ch);
				l_socketAddress[4]=charToUnsigned(&l_ch);
				l_socketAddress[5]=charToUnsigned(&l_ch);
				l_devPos=findByPoint(l_socketAddress,find_socketID);
				if(-1!=l_devPos)
				{
					ERROROUT("already have this socketAddress!");
					sem_post(&g_devList.st_equip.st_sem);///!!!!!
					goto socketFAILED;
				}
			}
			///no exist, add
			if(-1==addDev(pos))
			{
				ERROROUT("add device failed!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto databaseFAILED;
			}
			sem_wait(&(g_devList.st_empty.st_sem));///!!!!!
			if(-1==(l_devPos=getElem(g_devList.st_empty)))
			{
				sem_post(&(g_devList.st_equip.st_sem));///!!!!!
				sem_post(&g_devList.st_empty.st_sem);///!!!!!
				ERROROUT("add device failed!");
				delDev(pos);
				goto memoryFAILED;
			}
			sem_post(&(g_devList.st_empty.st_sem));				///!!!!!
			g_devList.st_equipArray[l_devPos].typeID=g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2];
			if(0x01==g_downTask.st_command[pos].param[1])		///socketID
			{
				memcpy(l_tempStr,&g_other.st_pCmdOther[4],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[0]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[6],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[1]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[8],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[2]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[10],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[3]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[12],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[4]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[14],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[5]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[16],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[6]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[18],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[7]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);

				g_devList.st_equipArray[l_devPos].socketStatus=sta_unknown;
				g_devList.st_equipArray[l_devPos].activeQueryStatus=sta_no;
			}
			else if(0x02==g_downTask.st_command[pos].param[1])	///operID
			{
				memcpy(l_tempStr,&g_other.st_pCmdOther[4],4);
				l_tempStr[4]=0;
				g_devList.st_equipArray[l_devPos].operID=(int)strtol(l_tempStr,(char**)NULL,16);
				g_devList.st_equipArray[l_devPos].equipmentID[0]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[1]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[3]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[4]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[5]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[6]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[7]=0;

			}
			else if(0x03==g_downTask.st_command[pos].param[1])	///socketID+operID
			{
				memcpy(l_tempStr,&g_other.st_pCmdOther[4],4);
				l_tempStr[4]=0;
				g_devList.st_equipArray[l_devPos].operID=(int)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[8],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[0]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[10],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[1]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[12],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[2]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[14],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[3]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[16],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[4]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[18],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[5]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[20],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[6]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[22],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[7]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);

				g_devList.st_equipArray[l_devPos].socketStatus=sta_unknown;
				g_devList.st_equipArray[l_devPos].activeQueryStatus=sta_no;
			}
			g_devList.st_equipArray[l_devPos].firstElec=equ_yes;
			///insert
			insertElem(g_devList.st_equip,l_devPos);
			///inquiry elec total power
			if((g_devList.st_equipArray[l_devPos].equipmentID[0]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[1]==0x00 \
					&& g_devList.st_equipArray[l_devPos].equipmentID[2]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[3]==0x00\
					&& g_devList.st_equipArray[l_devPos].equipmentID[4]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[5]==0x00)\
					|| ((g_devList.st_equipArray[l_devPos].typeID >> 12 & 0xf) == 0xf))
			{///make sure socket's address mustn't empty or not safeguard
				PRINTOUT("%s","no socket address, noNeed inquiry elec total power.");
			}
			else
			{
				g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
				g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
				g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x43;
				g_dataTrans.st_sendSerial[l_sendNum++]=0xc3;
				g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
				g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
				l_sendNum++;
			}
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
			///add successful
			sendService(err_no);
		}
		else if(0x02==g_downTask.st_command[pos].param[0])	///update device
		{
			PRINTOUT("%s","update device.");
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1==l_devPos)
			{
				ERROROUT("NO this device!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			if(-1==updateDev(pos))
			{
				ERROROUT("update device failed!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto databaseFAILED;
			}
			if(0x01==g_downTask.st_command[pos].param[1])		///operID
			{
				memcpy(l_tempStr,g_other.st_pCmdOther,4);
				l_tempStr[4]=0;
				g_devList.st_equipArray[l_devPos].operID=(int)strtol(l_tempStr,(char**)NULL,16);
			}
			else if(0x02==g_downTask.st_command[pos].param[1])	///socketID
			{
				memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[0]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[2],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[1]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[4],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[2]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[6],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[3]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[8],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[4]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[10],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[5]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[12],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[6]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[14],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[7]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);

				g_devList.st_equipArray[l_devPos].socketStatus=sta_unknown;
				g_devList.st_equipArray[l_devPos].activeQueryStatus=sta_no;
			}
			else if(0x07==g_downTask.st_command[pos].param[1])	///all
			{
				///operID
				memcpy(l_tempStr,&g_other.st_pCmdOther[4],4);
				l_tempStr[4]=0;
				g_devList.st_equipArray[l_devPos].operID=(int)strtol(l_tempStr,(char**)NULL,16);
				///socketID
				memcpy(l_tempStr,&g_other.st_pCmdOther[8],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[0]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[10],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[1]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[12],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[2]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[14],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[3]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[16],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[4]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[18],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[5]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[20],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[6]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				memcpy(l_tempStr,&g_other.st_pCmdOther[22],2);
				l_tempStr[2]=0;
				g_devList.st_equipArray[l_devPos].equipmentID[7]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				g_devList.st_equipArray[l_devPos].socketStatus=sta_unknown;
				g_devList.st_equipArray[l_devPos].activeQueryStatus=sta_no;
			}
			if(0x07==g_downTask.st_command[pos].param[1] || 0x02==g_downTask.st_command[pos].param[1])		///if change socket's address, inquiry total-power
			{
				g_devList.st_equipArray[l_devPos].firstElec=equ_yes;
				///inquiry elec total power
				if((g_devList.st_equipArray[l_devPos].equipmentID[0]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[1]==0x00 \
						&& g_devList.st_equipArray[l_devPos].equipmentID[2]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[3]==0x00\
						&& g_devList.st_equipArray[l_devPos].equipmentID[4]==0x00 && g_devList.st_equipArray[l_devPos].equipmentID[5]==0x00)\
						|| ((g_devList.st_equipArray[l_devPos].typeID >> 12 & 0xf) == 0xf))
				{///make sure socket's address mustn't empty
					PRINTOUT("%s","no socket address, noNeed inquiry elec total power.");
				}
				else
				{
					g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
					g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[0];
					g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[1];
					g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[2];
					g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[3];
					g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[4];
					g_dataTrans.st_sendSerial[l_sendNum++]=g_devList.st_equipArray[l_devPos].equipmentID[5];
					g_dataTrans.st_sendSerial[l_sendNum++]=0x68;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x02;
					g_dataTrans.st_sendSerial[l_sendNum++]=0x43;
					g_dataTrans.st_sendSerial[l_sendNum++]=0xc3;
					g_dataTrans.st_sendSerial[l_sendNum]=creatChecksum(l_sendNum);
					g_dataTrans.st_sendSerial[++l_sendNum]=0x16;
					l_sendNum++;
				}
			}
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
			sendService(err_no);
		}
		else if(0x03==g_downTask.st_command[pos].param[0])	///delete device
		{
			PRINTOUT("%s","delete device.");
			#if 0
			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
			if(-1==l_devPos)
			{
				ERROROUT("NO this device!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto socketFAILED;
			}
			if(-1==delDev(pos))
			{
				ERROROUT("delete device failed!");
				sem_post(&g_devList.st_equip.st_sem);///!!!!!
				goto databaseFAILED;
			}
			sem_wait(&(g_devList.st_empty.st_sem));///!!!!!
			if(-1==removeElem(g_devList.st_equip,l_devPos))
			{
				sem_post(&(g_devList.st_equip.st_sem));///!!!!!
				sem_post(&(g_devList.st_empty.st_sem));///!!!!!
				ERROROUT("delete device from memory failed!");
				goto memoryFAILED;
			}
			sem_post(&(g_devList.st_empty.st_sem));///!!!!!
			sem_post(&(g_devList.st_equip.st_sem));///!!!!!
			#endif
			sendService(err_no);
		}
		else if(0x04==g_downTask.st_command[pos].param[0])	///update mode
		{
			PRINTOUT("%s","update mode.");
			if(-1==updateMode(pos))
			{
				ERROROUT("update mode failed!");
				goto databaseFAILED;
			}
			else
				sendService(err_no);
		}
		else if(0x05==g_downTask.st_command[pos].param[0])	///delete mode
		{
			PRINTOUT("%s","delete mode.");
			if(-1==delMode(pos))
			{
				ERROROUT("delete mode failed!");
				goto databaseFAILED;
			}
			else
				sendService(err_no);
		}
		else if(0x06==g_downTask.st_command[pos].param[0])	///delete time-task
		{
			PRINTOUT("%s","delete time-task.");
			int l_controlValue;
			memcpy(l_tempStr,&g_other.st_pCmdOther[0],4);		///controlVal
			l_tempStr[4]=0;
			l_controlValue=strtol(l_tempStr,(char**)NULL,16);

			sem_wait(&g_devList.st_equip.st_sem);///!!!!!
			if((l_controlValue>>8 & 0xff)== 0xfe)
			{
				l_devPos=-1;									///do nothing
			}
			else
			{
				l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
				if(-1==l_devPos)
				{
					ERROROUT("NO this device!");
					sem_post(&(g_devList.st_equip.st_sem));///!!!!!
					goto socketFAILED;
				}
			}
			/* 2012-03-10 V1.0.5 modify by LiYang begin */
			///delete from memory
			int l_controlTime;
			int frequency_today,frequency_cmd;
			struct timeval l_time1,l_time2;
			memcpy(l_tempStr,&g_other.st_pCmdOther[6],10);		///controlTime
			l_tempStr[10]=0;
			l_controlTime=atoi(l_tempStr);
			if(-1==gettimeofday(&l_time2,NULL))
			{
				ERROROUT("get-time failed!");
				goto otherFAILED;
			}
			l_time2.tv_sec=l_time2.tv_sec+8*3600;
			frequency_today=localtime(&l_time2.tv_sec)->tm_wday;	//today frequency
			if ( 0 == frequency_today )
				frequency_today=7;
			memcpy(l_tempStr,&g_other.st_pCmdOther[4],2);
			l_tempStr[2]=0;
			frequency_cmd=strtol(l_tempStr,(char**)NULL,16);		//cmd frequency
			if ( (0 != frequency_cmd) && (1 != frequency_cmd) && (1 != (frequency_cmd >> frequency_today & 0x1)) )		//check frequency_today if belongs frequency_cmd
			{
				printf("needn't delete from memory\n");
			}
			else
			{
				sem_wait(&g_downTask.st_timeTask.st_sem);///!!!!!
				for(int i=g_downTask.st_timeTask.head;i!=-1;i=g_downTask.st_command[i].next)
				{
					l_time1.tv_sec=g_downTask.st_command[i].controlTime;
					if((g_downTask.st_command[i].param[0]*256+g_downTask.st_command[i].param[1]==l_controlValue) && \
							g_downTask.st_command[i].controlTime%(3600*24) == l_controlTime%(3600*24))
					{
						sem_wait(&g_downTask.st_emtpy.st_sem);///!!!!!
						removeElem(g_downTask.st_timeTask,i);
						sem_post(&g_downTask.st_emtpy.st_sem);///!!!!!
						break;
					}
				}
				sem_post(&g_downTask.st_timeTask.st_sem);///!!!!!
			}
			/* 2012-03-10 V1.0.5 modify by LiYang end */
			///delete from database
			if(-1==delTask(l_devPos))
			{
				ERROROUT("delete time-task failed!");
				sem_post(&(g_devList.st_equip.st_sem));///!!!!!
				goto databaseFAILED;
			}
			else
				sendService(err_no);
			sem_post(&(g_devList.st_equip.st_sem));///!!!!!
		}
		else if(0x07==g_downTask.st_command[pos].param[0])	///modify time-task
		{
			PRINTOUT("%s","modify time-task.");
			int l_controlVal,l_controlTime,l_oldcontrolVal,l_oldcontrolTime;
			memcpy(l_tempStr,&g_other.st_pCmdOther[0],4);		///old-controlVal
			l_tempStr[4]=0;
			l_oldcontrolVal=strtol(l_tempStr,(char**)NULL,16);
			memcpy(l_tempStr,&g_other.st_pCmdOther[16],4);		///new-controlVal
			l_tempStr[4]=0;
			l_controlVal=strtol(l_tempStr,(char**)NULL,16);
			memcpy(l_tempStr,&g_other.st_pCmdOther[6],10);		///old-controlTime
			l_tempStr[10]=0;
			l_oldcontrolTime=atoi(l_tempStr);
			memcpy(l_tempStr,&g_other.st_pCmdOther[22],10);		///new-controlTime
			l_tempStr[10]=0;
			l_controlTime=atoi(l_tempStr);

			sem_wait(&(g_devList.st_equip.st_sem));///!!!!!
			if((l_controlVal>>8 & 0xff)== 0xfe)
			{
				l_devPos=-1;									///mode operation ,do nothing
			}
			else
			{
				l_devPos=findByID(g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2],find_typeID);
				if(-1==l_devPos)
				{
					ERROROUT("NO this device!");
					sem_post(&(g_devList.st_equip.st_sem));///!!!!!
					goto socketFAILED;
				}
			}
			///modify from memory
			sem_wait(&g_downTask.st_timeTask.st_sem);///!!!!!
			for(int i=g_downTask.st_timeTask.head;i!=-1;i=g_downTask.st_command[i].next)
			{
				if((g_downTask.st_command[i].param[0]*256+g_downTask.st_command[i].param[1]==l_oldcontrolVal) && \
						g_downTask.st_command[i].controlTime==l_oldcontrolTime && (-1==l_devPos || (\
						g_downTask.st_command[i].typeID[0]==g_downTask.st_command[pos].typeID[0] && \
						g_downTask.st_command[i].typeID[1]==g_downTask.st_command[pos].typeID[1] && \
						g_downTask.st_command[i].typeID[2]==g_downTask.st_command[pos].typeID[2])))
				{
					struct timeval l_tv;
					if(-1==gettimeofday(&l_tv,NULL))
					{
						ERROROUT("get-time failed!");
						goto otherFAILED;
					}
					///adjust time stamp
					l_tmpTime=getTodayZero(l_tv.tv_sec);
					if(l_controlTime>l_tv.tv_sec && (l_controlTime<=(l_tmpTime+3600*24)))
					{
						g_downTask.st_command[i].param[0]=l_controlVal>>8 & 0xff;
						g_downTask.st_command[i].param[1]=l_controlVal & 0xff;
						g_downTask.st_command[i].controlTime=l_controlTime;
					}
					else
					{
						sem_wait(&g_downTask.st_emtpy.st_sem);///!!!!!
						removeElem(g_downTask.st_timeTask,i);
						sem_post(&g_downTask.st_emtpy.st_sem);///!!!!!
					}
					break;
				}
			}
			sem_post(&g_downTask.st_timeTask.st_sem);///!!!!!
			///modify from database
			if(-1==modifyTask(l_devPos))
			{
				ERROROUT("modify time-task failed!");
				sem_post(&(g_devList.st_equip.st_sem));///!!!!!
				goto databaseFAILED;
			}
			else
				sendService(err_no);
			sem_post(&(g_devList.st_equip.st_sem));///!!!!!
		}
		else
		{
			ERROROUT("no this format command!");
			goto formatFAILED;
		}
	}
	else if(0x04==g_downTask.st_command[pos].cmdType)		///other
	{
		PRINTOUT("%s","other kind command.");
		if(0x01==g_downTask.st_command[pos].param[0])		///set system-time-clock
		{
			struct timeval l_tv;
			if(0x01==g_downTask.st_command[pos].param[1])
			{
				struct tm *p_UTC;
				time_t timep;
				PRINTOUT("%s","set time1234.");
			
				
				memcpy(l_tempStr, &g_other.st_pCmdOther[0], 10);
				l_tempStr[10]='\0';
				PRINTOUT("%s",l_tempStr);
				
				timep=atoi(l_tempStr);
				PRINTOUT("liuyongfei=%d",timep);
				#if 0
				p_UTC=localtime(&timep);
				///st_pCmdOther
				sprintf(l_tempStr,"%02d",p_UTC->tm_mon+1);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				sprintf(l_tempStr,"%02d",p_UTC->tm_mday);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				//{
					//if(p_UTC->tm_hour > 16)
					//p_UTC->tm_hour = p_UTC->tm_hour - 16;
					//else
					//p_UTC->tm_hour = p_UTC->tm_hour + 8;
				//}	
				sprintf(l_tempStr,"%02d",p_UTC->tm_hour);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				sprintf(l_tempStr,"%02d",p_UTC->tm_min);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				sprintf(l_tempStr,"%04d",p_UTC->tm_year+1900);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
				g_other.st_pCmdOther[l_sendNum++]='.';
				sprintf(l_tempStr,"%02d",p_UTC->tm_sec);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				g_other.st_pCmdOther[l_sendNum++]=0;

				//sprintf(l_tempStr,"date %s;hwclock -w;",g_other.st_pCmdOther);// charlie modified,need to support hwclock later
				sprintf(l_tempStr,"date %s;",g_other.st_pCmdOther);
				PRINTOUT("liu%s",l_tempStr);
#if 0
				if(0!=system(l_tempStr))
				{
					PRINTOUT("%s","set time failed!");
					goto otherFAILED;
				}
#endif
				#endif
				sendService(err_no);
				
				
				//if(-1==gettimeofday(&l_tv,NULL))
				//{
					//PRINTOUT("%s","get-time failed!");
					//goto otherFAILED;
				//}

				//PRINTOUT("yong%u",l_tv.tv_sec);
				
				///delete data from database
				delDataByChTime(timep);
				///change dead-time task
				g_deadTimeTask.st_inquiryElec=timep/(ELECGETINTERVAL)*(ELECGETINTERVAL)+ELECGETPOINT;			///inquiry elec total-power
				g_deadTimeTask.st_inquiryYGGL=timep/(YGGLGETINTERVAL)*(YGGLGETINTERVAL)+YGGLGETPOINT;        ///define inquiry active power time
				///adjust time stamp
				l_tmpTime=getTodayZero(timep);
				PRINTOUT("fei%u",l_tmpTime);
				g_deadTimeTask.st_syncTimeTask=l_tmpTime+5;
				///change delay-base
				l_tv.tv_sec = timep;
				l_tv.tv_usec = 0;
				g_sendDelay.st_lastSend=l_tv;
				///change time set flag for socket
				l_sendNum=0;
				flagAddress=0;
			}
			else if(0x02==g_downTask.st_command[pos].param[1])
			{
				PRINTOUT("%s","get time.");
				if(-1==gettimeofday(&l_tv,NULL))
				{
					ERROROUT("get-time failed!");
					goto otherFAILED;
				}
				sprintf(l_tempStr,"%010u",(unsigned int)l_tv.tv_sec);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[6];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[7];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[8];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[9];
				g_other.st_pCmdOther[l_sendNum++]='#';
				g_other.st_pCmdOther[l_sendNum++]='\0';
				sendService(send_only);
				l_sendNum=0;
			}
		}
		else if(0x02==g_downTask.st_command[pos].param[0])	///safeguard
		{
			PRINTOUT("%s","safeguard,not support!");
		}
		else if(0x04==g_downTask.st_command[pos].param[0])	///program version
		{
			PRINTOUT("%s","program version.");
			int l_version;
			l_version=getGlobalPara((char*)"version");
			if(-1==l_version)
			{
				ERROROUT("version failed!");
				goto databaseFAILED;
			}
			else
			{
				sprintf(l_tempStr,"%08d",l_version);
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[6];
				g_other.st_pCmdOther[l_sendNum++]=l_tempStr[7];
				g_other.st_pCmdOther[l_sendNum++]='#';
				g_other.st_pCmdOther[l_sendNum++]='\0';
				sendService(send_only);
				l_sendNum=0;
			}
		}
		else if(0x05==g_downTask.st_command[pos].param[0])		  					  ///Media device OperID inquiry
		{
			if(0x82==g_downTask.st_command[pos].typeID[0])
			{
				PRINTOUT("%s","'Media OperID' set.");
				g_dataTrans.st_sendSerial[l_sendNum++]=0x09;
				g_dataTrans.st_sendSerial[l_sendNum++]=0xf0;
				if('#'==g_other.st_pCmdOther[4])
				{
					memcpy(l_tempStr,&g_other.st_pCmdOther[2],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
					memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);
					l_tempStr[2]=0;
					g_dataTrans.st_sendSerial[l_sendNum++]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
				}
				else
				{
					ERROROUT("socket format error!");
					goto formatFAILED;
				}
				g_dataTrans.st_sendSerial[l_sendNum++]=0x34;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x12;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x00;
				g_dataTrans.st_sendSerial[l_sendNum++]=0x01;
			}
		}
	}

	if(0!=l_sendNum)										///have data to send
	{
		myDelay(needDelay(0));
		if(!sendSerial(l_sendNum))
		{
			ERROROUT("sendSerial() failed!");
			goto serialFAILED;
		}
	}
	return 0;

	lightCONTROL:											///send data; light-control
//	sendSerial(l_sendNum);
	if(0!=l_sendNum)
	{
		for (int i=0;i<30;i++)
		{
			myDelay(60);
			if(!sendSerial(l_sendNum))
			{
				ERROROUT("sendSerial() failed!");
				goto serialFAILED;
			}
		}
	}
	return 0;

	formatFAILED:
	sendService(err_formatpro);
	return -1;

	socketFAILED:
	sendService(err_socket);
	return -1;

	databaseFAILED:
	sendService(err_database);
	return -1;

	memoryFAILED:
	sendService(err_memory);
	return -1;

	serialFAILED:
	sendService(err_serial);
	return -1;

	otherFAILED:
	sendService(err_other);
	return -1;
}
